package array;
//除自身以外数组的乘积
public class leetcode238 {
    public static void main(String[] args) {
        int[] nums = {1,2,3,4};
        int[] result = productExceptSelf(nums);
        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i] + " ");
        }
    }

    public static int[] productExceptSelf(int[] nums) {
        int[] result = new int[nums.length];
        int[] left = new int[nums.length];
        int[] right = new int[nums.length];
        int res = 1;
        left[0] = 1;
        right[nums.length - 1] = 1;

        for (int i = 1; i < left.length; i++) {
            res = res * nums[i-1];
            left[i] = res;
        }
        res = 1;
        for (int i = left.length - 2; i >= 0; i--){
            res = res * nums[i+1];
            right[i] = res;
        }
        for (int i = 0; i < result.length; i++) {
            result[i] = left[i] * right[i];
        }

        return result;
    }
}
